package com.cn.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.cn.enums.ErrorMsg;

/**
 * 统一返回对象
 */
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Result<T> {

    private Integer status_code;
    private String msg;
    private T data;

    // 直接返回成功状态码
    public static Result success(){
        Result result = new Result();
        result.setStatus_code(1);
        return result;
    }

    // 返回成功状态码的同时返回对象
    public static <T> Result success(T data){
        Result<T> result = new Result<>();
        result.setStatus_code(1);
        result.setData(data);
        return result;
    }

    // 直接返回错误状态码和错误信息
    public static Result fail(ErrorMsg errorMsg){
        Result result = new Result();
        result.setStatus_code(0);
        result.setMsg(errorMsg.getMsg());
        return result;
    }

    // 返回错误状态码和错误信息的同时返回错误对象
    public static <T> Result fail(ErrorMsg errorMsg, T data){
        Result<T> result = new Result<>();
        result.setStatus_code(0);
        result.setMsg(errorMsg.getMsg());
        result.setData(data);
        return result;
    }

    public Integer getStatus_code() {
        return status_code;
    }

    public void setStatus_code(Integer status_code) {
        this.status_code = status_code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result(Integer status_code, String msg, T data) {
        this.status_code = status_code;
        this.msg = msg;
        this.data = data;
    }

    public Result() {
    }
}
